package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.basic.ProductQualityConfig;
import com.canaan.business.mapper.basic.ProductQualityConfigMapper;
import com.canaan.business.service.basic.IProductQualityConfigService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * 物料质检相关配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-07
 */
@Service
public class ProductQualityConfigServiceImpl implements IProductQualityConfigService {
    @Resource
    private ProductQualityConfigMapper productQualityConfigMapper;

    /**
     * 查询物料质检相关配置
     *
     * @param id 物料质检相关配置主键
     * @return 物料质检相关配置
     */
    @Override
    public ProductQualityConfig selectProductQualityConfigById(Long id) {
        return productQualityConfigMapper.selectProductQualityConfigById(id);
    }

    /**
     * 查询物料质检相关配置列表
     *
     * @param productQualityConfig 物料质检相关配置
     * @return 物料质检相关配置
     */
    @Override
    public List<ProductQualityConfig> selectProductQualityConfigList(ProductQualityConfig productQualityConfig) {
        return productQualityConfigMapper.selectProductQualityConfigList(productQualityConfig);
    }

    /**
     * 新增物料质检相关配置
     *
     * @param productQualityConfig 物料质检相关配置
     * @return 结果
     */
    @Override
    public int insertProductQualityConfig(ProductQualityConfig productQualityConfig) {
        if(StringUtils.isBlank(productQualityConfig.getNo())){
            productQualityConfig.setNo(TableNoUtil.getNo(ProductQualityConfig.class));
        }
        productQualityConfig.setCreateBy(SecurityUtils.getUsername());
        productQualityConfig.setCreateTime(DateUtils.getNowDate());
        return productQualityConfigMapper.insertProductQualityConfig(productQualityConfig);
    }

    /**
     * 批量新增物料质检相关配置
     *
     * @param productQualityConfigList 物料质检相关配置列表
     * @return 结果
     */
    @Override
    public int batchInsertProductQualityConfig(List<ProductQualityConfig> productQualityConfigList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ProductQualityConfig productQualityConfig : productQualityConfigList){
            if(StringUtils.isBlank(productQualityConfig.getNo())){
                productQualityConfig.setNo(TableNoUtil.getNo(ProductQualityConfig.class));
            }
            productQualityConfig.setCreateBy(username);
            productQualityConfig.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<productQualityConfigList.size();) {
            int endIndex = i+500;
            if (endIndex > productQualityConfigList.size()) {
                endIndex = productQualityConfigList.size();
            }
            insertNum = insertNum + productQualityConfigMapper.batchInsertProductQualityConfig(productQualityConfigList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改物料质检相关配置
     *
     * @param productQualityConfig 物料质检相关配置
     * @return 结果
     */
    @Override
    public int updateProductQualityConfig(ProductQualityConfig productQualityConfig) {
        productQualityConfig.setUpdateBy(SecurityUtils.getUsername());
        productQualityConfig.setUpdateTime(DateUtils.getNowDate());
        return productQualityConfigMapper.updateProductQualityConfig(productQualityConfig);
    }

    /**
     * 批量修改物料质检相关配置
     *
     * @param productQualityConfigList 物料质检相关配置列表
     * @return 结果
     */
    @Override
    public int batchUpdateProductQualityConfig(List<ProductQualityConfig> productQualityConfigList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ProductQualityConfig productQualityConfig : productQualityConfigList){
            productQualityConfig.setUpdateBy(username);
            productQualityConfig.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<productQualityConfigList.size();) {
            int endIndex = i+500;
            if (endIndex > productQualityConfigList.size()) {
                endIndex = productQualityConfigList.size();
            }
            updateNum = updateNum + productQualityConfigMapper.batchUpdateProductQualityConfig(productQualityConfigList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除物料质检相关配置
     *
     * @param ids 需要删除的物料质检相关配置主键集合
     * @return 结果
     */
    @Override
    public int deleteProductQualityConfigByIds(Long[] ids) {
        return productQualityConfigMapper.deleteProductQualityConfigByIds(ids);
    }

    /**
     * 删除物料质检相关配置信息
     *
     * @param id 物料质检相关配置主键
     * @return 结果
     */
    @Override
    public int deleteProductQualityConfigById(Long id) {
        return productQualityConfigMapper.deleteProductQualityConfigById(id);
    }

    /**
     * 查询物料质检相关配置
     *
     * @param no 编号
     * @return 物料质检相关配置
     */
    @Override
    public ProductQualityConfig selectProductQualityConfigByNo(String no) {
        return productQualityConfigMapper.selectProductQualityConfigByNo(no);
    }

    /**
     * 根据物料编号查询物料质检相关配置
     *
     * @param productNo 物料编号
     * @return 物料质检相关配置
     */
    @Override
    public ProductQualityConfig getByProductNo(String productNo){
        return productQualityConfigMapper.getByProductNo(productNo);
    }

    /**
     * 批量删除物料质检相关配置
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteProductQualityConfigByNos(String[] nos) {
        return productQualityConfigMapper.deleteProductQualityConfigByNos(nos);
    }

    /**
     * 删除物料质检相关配置信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteProductQualityConfigByNo(String no) {
        return productQualityConfigMapper.deleteProductQualityConfigByNo(no);
    }

    /**
     * 设置物料质检配置
     *
     * @param productQualityConfig 参数
     * @return 结果
     */
    @Override
    public int setQualityConfig(ProductQualityConfig productQualityConfig){
        List<String> productNos = productQualityConfig.getProductNos();
        for (String productNo : productNos) {
            ProductQualityConfig configItem = this.getByProductNo(productNo);
            // 如果原先不存在，新增
            if (configItem == null){
                configItem = new ProductQualityConfig();
                configItem.setProductNo(productNo);
                configItem.setWhetherPass(productQualityConfig.getWhetherPass());
                configItem.setWhetherWaitStock(productQualityConfig.getWhetherWaitStock());
                configItem.setOverduePeriod(productQualityConfig.getOverduePeriod());
                this.insertProductQualityConfig(configItem);
            }
            // 如果原先存在，编辑
            else {
                configItem.setWhetherPass(productQualityConfig.getWhetherPass());
                configItem.setWhetherWaitStock(productQualityConfig.getWhetherWaitStock());
                configItem.setOverduePeriod(productQualityConfig.getOverduePeriod());
                this.updateProductQualityConfig(configItem);
            }
        }
        return 1;
    }
}
